package com.database.controller;

import com.database.character.Collect;
import com.database.character.Result;
import com.database.service.CollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/collects")
@Api(value = "收藏controller",tags = {"收藏相关接口"})
public class CollectController {
	@Autowired
	private CollectService collectService;
	/*添加收藏*/
	@PostMapping
	@ApiOperation(value = "添加收藏",notes = "添加收藏",httpMethod = "POST")
	@ApiResponse(code = 200, message = "添加成功！", response = Result.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "token",value = "token",required = true,dataTypeClass = String.class,paramType = "header")
	})
	public Result addCollect(@RequestBody Collect collect){
		Integer userId = collect.getUserId();
		Integer postId = collect.getPostId();
		int i = collectService.addCollect(collect);
		if(i==0){
			return Result.error("收藏失败");
		}
		else{
			Collect collectByUidAndPid = collectService.getCollectByUidAndPid(userId, postId);
			return Result.success("收藏成功",collectByUidAndPid);
		}
	}
	/*取消收藏*/
	@DeleteMapping("/{collectId}")
	@ApiOperation(value = "取消收藏",notes = "取消收藏",httpMethod = "DELETE")
	@ApiResponse(code = 200, message = "取消成功！", response = Result.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "collectId",value = "收藏id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	public Result deleteCollect(@PathVariable Integer collectId){
		int i = collectService.deleteByCollectId(collectId);
		if(i==0){
			return Result.error("删除失败");
		}
		else{
			return Result.success("删除成功",null);
		}
	}

	@DeleteMapping("/{userId}/{postId}")
	@ApiOperation(value = "取消收藏",notes = "取消收藏",httpMethod = "DELETE")
	@ApiResponse(code = 200, message = "取消成功！", response = Result.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId",value = "用户id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "postId",value = "帖子id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	public Result deleteCollectByUidAndPid(@PathVariable Integer userId,@PathVariable Integer postId){
		int i = collectService.deleteByUidAndPid(userId, postId);
		if(i==0){
			return Result.error("删除失败");
		}
		else{
			return Result.success("删除成功",null);
		}
	}

	/*查看收藏*/
	@GetMapping("/{userId}/{page}/{limit}")
	@ApiOperation(value = "查看收藏",notes = "查看收藏",httpMethod = "GET")
	@ApiResponse(code = 200, message = "查询成功！", response = Result.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId",value = "用户id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "page",value = "页码",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "limit",value = "每页条数",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	public Result getCollect(@PathVariable(value = "userId") Integer userId,@PathVariable(value = "page" ,required = false) Integer pageNum,
							 @PathVariable(value = "limit" ,required = false) Integer pageSize){
		PageHelper.startPage(pageNum,pageSize);//分页
		List<Collect> collectByUid = collectService.getCollectByUid(userId);
		PageInfo list = new PageInfo(collectByUid);
		Map<String,Object> map = new HashMap<>();
		map.put("total",list.getTotal());
		map.put("data",list.getList());
		return Result.success("查询成功",map);
	}

	@GetMapping("/{userId}/{postId}")
	@ApiOperation(value = "查看收藏",notes = "查看收藏",httpMethod = "GET")
	@ApiResponse(code = 200, message = "查询成功！", response = Result.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId",value = "用户id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "postId",value = "帖子id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	public Result getCollectByUidAndPid(@PathVariable Integer userId,@PathVariable Integer postId){
		Collect collectByUidAndPid = collectService.getCollectByUidAndPid(userId, postId);
		return Result.success("查询成功",collectByUidAndPid);
	}
}
